using System.Net.Mail;
using System.Collections.Generic;

using GSN.Senders;
using GSN.Mail.Messages;

namespace GSN.Mail
{
    class POP3Manager : IMailManager
    {
        private string _username = null;
        private string _password = null;
        private string _server = null;
        private int _port = -1;

        //private List<string> prevMsgs = new List<string>();
        private Message[] msgs = null;

        public POP3Manager(string username, string password, string server, int port)
        {
            _username = username;
            _password = password;
            _server = server;
            _port = port;
        }

        public Message[] getData()
        {
            POP3Client pop3 = new POP3Client();

            pop3.connect(_server, _port);
            pop3.login(_username, _password);
            msgs = pop3.peek();
            pop3.disconnect();
            return msgs;
        }

        public override bool Equals(object obj)
        {
            if (obj is IMailManager)
            {
                if (obj is POP3Manager)
                {
                    POP3Manager m = (POP3Manager)obj;
                    if (m._username == this._username &&
                            m._password == this._password &&
                                m._server == this._server &&
                                    m._port == this._port)
                    {
                        return true;
                    }
                }
                else
                    return false;
                
            }
            else
                throw new System.ArgumentException("Argument is object not a IMailManager");

            return false;
        }

        public override int GetHashCode()
        {
            return (_username.GetHashCode() + _password.GetHashCode() + _server.GetHashCode() + _port.GetHashCode());
        }

        /*
        public void notifyNewMessage(ISMSSender sender)
        {
            string str = "";

            // don't want to bother sender if no entries
            if (msgs == null || msgs.Length == 0)
                return;

            sender.start();

            foreach (Message msg in msgs)
            {
                // entry.id - need to check if unique
                if (!prevMsgs.Contains(msg.messageId))
                {
                    str = msg.getSummary();

                    Logger.log("Sending SMS: " + msg);

                    sender.send(GSNConfig.getConfigItem(GSNConfig.GSN_CONFIG_PHONE), str);

                    prevMsgs.Add(msg.messageId);
                }
            }

            sender.stop();
        }
         * */
    }
}
